<?php
/**
 * americanfocuscontroller.php
 * Create on 2016-03-29 16:18:49
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\channel\american;


use haibao\cms\web\view\app\article\Preview;
class AmericanFocusController extends \haibao\cms\business\control\AmericanController{
	
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		
		
		
	   //当红剧星
	    list($articles,$imageColls) = $this->getArticleInfo(3); //剧星动态下的文章
	    $tagIdArr = array();
	    foreach($imageColls['collTag'] as $key=>$val){
	       foreach($val as $k=>$v){
	           array_push($tagIdArr,$v['TagId']);
	       }   
	    }
	    foreach($articles['articleTag'] as $key=>$val){
	        foreach($val as $k=>$v){
	            array_push($tagIdArr,$v['TagId']);
	        }
	    }
	    if($tagIdArr){
	        $countArr = array_count_values($tagIdArr); //每个tagid的次数数组
	        $starAllArr = array();
	        list($starAllArr,$starTagIds) = $this->getStarTagIds($tagIdArr);
	        $starInfo = \haibao\cms\business\Celebrity::getCelebrityProInfoByTagIds($starTagIds);
	        if($starAllArr){
	            $starAllArr = $this->getFunctionData($starAllArr,$starInfo,$countArr);
	        }
	        $count = array();
	        foreach ($starAllArr as $star) {
	            $count[] = $star['count'];
	        }
	        array_multisort($count, SORT_DESC, $starAllArr);
	        $starAllArr = array_slice($starAllArr,0,2);
	       
	    }
	    $this->assign('starAllArr', $starAllArr);
	    
		
		$parameters = $this->getControlParameters($controlData);
		$this->assign('parameters', $parameters);
		$this->assign('userHostColler', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_USER));
		$this->generateHtml($path, $content);
		return $this->getHtml($path);
	}
	
	private function getStarTagIds($tagArr){
	    $tagInfo = \haibao\cms\business\Tag::getTagsById($tagArr);
	    $starArr = $starAllArr = $starTagIds = array();
	    foreach($tagInfo as $key=>$val){
	        if($val['TagType'] == 4){
	            $starArr['Id'] = $val['Id'];
	            $starArr['Url'] = $val['Url'];
	            array_push($starAllArr,$starArr);  //明星标签数组   $countArr进行循环  找出次数多的两个明星
	            array_push($starTagIds,$val['Id']);
	        }
	    }
	    return array($starAllArr,$starTagIds);
	}
	
	
	private function getFunctionData($starAllArr,$starInfo,$countArr){
	    foreach($starAllArr as $key=>$val){
	        if(isset($countArr[$val['Id']])){
	            $starAllArr[$key]['count'] = $countArr[$val['Id']];
	        }
	        foreach($starInfo as $k=>$v){
	            if($val['Id'] == $v->TagId){
	                $starAllArr[$key]['NameCn'] = $v->NameCn;
	                $starAllArr[$key]['NameEn'] = $v->NameEn;
	                $starAllArr[$key]['RepWorks'] = $v->CelebrityInfo->RepWorks;
	                $starAllArr[$key]['ImageId'] = $v->CelebrityImage->ImageId;
	                $starImgArr[$val['Id']] = $v->CelebrityImage->ImageId;
	            }
	        }
	    }
	    $imgUrlArr = \haibao\cms\business\ImageLibrary::getImageUrlById($starImgArr);
	    foreach($starAllArr as $key=>$val){
	        if(isset($val['ImageId']) && isset($imgUrlArr[$val['ImageId']])){
	            $starAllArr[$key]['ImageUrl'] = self::getImageLink($imgUrlArr[$val['ImageId']],100,100);
	        }
	        $starAllArr[$key]['Url'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STARS).$val['Url'];
	    }
	    return $starAllArr;
	}
}